/**
 * 
 */

   function initMap(mapid){
	   
	   var geoc = new BMap.Geocoder(); 
	   var  map = new BMap.Map(mapid);
	   
		var point = new BMap.Point(116.404, 39.915);
		
		
		var size = new BMap.Size(10, 20);
		  map.addControl(new BMap.CityListControl({
		        anchor: BMAP_ANCHOR_TOP_LEFT,
		        offset: size,
		        // 切换城市之间事件
		        // onChangeBefore: function(){
		        //    alert('before');
		        // },
		        // 切换城市之后事件
		        // onChangeAfter:function(){
		        //   alert('after');
		        // }
		    }));
		 // map.addControl(new BMap.NavigationControl());               // 添加平移缩放控件
	    map.addControl(new BMap.ScaleControl());                    // 添加比例尺控件
	    map.addControl(new BMap.OverviewMapControl());              //添加缩略地图控件
		map.enableScrollWheelZoom();                            //启用滚轮放大缩小
		map.addControl(new BMap.MapTypeControl());          //添加地图类型控件
		map.disable3DBuilding();
		map.centerAndZoom(point, 14); 
	//    map.setMapType(new BMap.MapType("B_DIMENSIONAL_MAP"));// 地图类型怎么设置
		//代码使用如下,即可. 模板页可以查看http://developer.baidu.com/map/custom/list.htm      
		//map.setMapStyle({style:'midnight'});
	  
		//changeMapStyle('midnight')
		//sel.value = 'midnight';
		$(".anchorBL").css("display:none");
	
/* 			function changeMapStyle(style){
			map.setMapStyle({style:style});
			$('#desc').html(mapstyles[style].desc);
		}
		 */
//		$("body").on(
//    			"click",
//    			".level_2>.name",
//    			function() {
//    			return false;
//    			});
		
		 
	/*	 $("body").on("click",".level_2>.name",function(){
			 var _this=$(this).parent() ;
			 if(_this.hasClass("active")){
				 _this.removeClass("active");
				 return;
			 }
			 $(".level_2.active").removeClass("active"); 
			 _this.addClass("active");
			 //设置地图的中心位置为当前选中的点
			 
			   var  gtar=_this.children(".glocation");
			   var  points=gtar.val().split(",");
			   if(points.length<2) {
				   var msg={success:false,desc:"该设备坐标参数不全！"}
				 //  showTipInfo(msg);
			   }
			   else
			   map.panTo(new BMap.Point(points[0],points[1]));
			 //设置marker 文本的 高亮联动选中
	        var mid="#m_"+_this.attr("puid");
	        $(".mapInfo.active").removeClass("active");
	        
	        $(mid).addClass('active');
	    	$(".ds_tool .title").html(_this.children(".name").html());
	    	 
			 return false;
		 });*/
		 
		 
		 //创建坐标点
		function showInfo(e){
			//alert(e.point.lng + ", " + e.point.lat);
			var tar=  $(".level_2.active");
			if(tar.length>0){
				tar.removeClass("active");
				
				    var marker = tar.data("marker");
				    if(marker==undefined)
			    	{
				    	marker=	addMarker(map,e.point,tar.children(".name").html(),tar.attr("puid"));
				   /*   marker=	new BMap.Marker(e.point);  // 创建标注  
						map.addOverlay(marker);              // 将标注添加到地图中
						marker.addEventListener("mouseover",getAttr);
						function getAttr(){
							var p = marker.getPosition();       //获取marker的位置
							 alert("marker的位置是" + p.lng + "," + p.lat);   
						} */ 
						tar.data("marker",marker);
			    	}else {
			    		marker.setPosition(e.point);
			    	}
			}
			
			geoc.getLocation(e.point, function(rs){
				var addComp = rs.addressComponents;
				console.dir(addComp.province + ", " + addComp.city + ", " + addComp.district + ", " + addComp.street + ", " + addComp.streetNumber);
			});
		}
		//动态设置坐标
		function setGISval(e){
		//	alert(e.point.lng + ", " + e.point.lat);
			$(".level_2.active .glocation").val(e.point.lng + ", " + e.point.lat)
		
			
		} 
		 map.bindEvent=function(){
			    map.addEventListener("click", showInfo);
				
			    map.addEventListener("mousemove", setGISval);
		   }
		
		return   map;
   }
   
   initMap.bindEvent=function(){
	   this.map.addEventListener("click", showInfo);
		
	   this.map.addEventListener("mousemove", setGISval);
   }

// 复杂的自定义覆盖物
    function ComplexCustomOverlay(point, text, id){
      this._point = point;
      this._text = text;
      this._id = id;
    }
    ComplexCustomOverlay.prototype = new BMap.Overlay();
    ComplexCustomOverlay.prototype.initialize = function(map){
	      this._map = map;	  
		  var div= $("<div class='mapInfo' id='m_"+this._id+"'><span class='mapInfo_icon'></span><div class='mapInfo_layout'><div class='m_title'>"+this._text+"</div><div class='m_body'><ul><li> </li><li> </li></ul></div></div></div>")[0];
	      this._div=div;
	      this._map.getPanes().labelPane.appendChild(div);  
	      return div;
    }
     ComplexCustomOverlay.prototype.setPosition=function (point){
        this._point=point;
//    	 http://api0.map.bdimg.com/images/blank.gif
       this.draw();
     }
 
    ComplexCustomOverlay.prototype.draw = function(){
	/////////////////
      var map = this._map;
 	  var zoom =map.getZoom();
//	   //先draw 后 zoom 事件，需要在draw 中调整大学奥
//	    if(zoom>16)
//		   {
//		     $(".mapInfo_layout").attr("class","mapInfo_layout infoBig");
//		   }
//		   else if (zoom<=14)
//		   {
//			$(".mapInfo_layout").attr("class","mapInfo_layout infoSmall");
//		   }
//		   else{
//
//		   $(".mapInfo_layout").attr("class","mapInfo_layout infoNormal");
//		   }

      var pixel = map.pointToOverlayPixel(this._point);
	 
      this._div.style.left = pixel.x + 10 + "px";
	//  var y=$(".mapInfo_layout").height();//+30; //$(".mapInfo_layout").height()是动态的不准确
      this._div.style.top  = (pixel.y -30 ) + "px";    
    }
    
	// 编写自定义函数,创建标注
	function addMarker(map,point,text,id){
//	  var marker = new BMap.Marker(point);
   //    var label = new BMap.Label("<div class='mapInfo'><div class='m_title'>"+text+"</div><div class='m_body'><ul><li>湿度</li><li>温度</li></ul></div></div>",{offset:new BMap.Size(20,-110)});
   /* map.addEventListener("zoomend",function(a,b){
        if( this.getZoom()>15)
		  {
			label.show();
		  
		  }
		  else 
	      label.hide(); 
	   });
      marker.setLabel(label);*/

	   var myCompOverlay = new ComplexCustomOverlay(point, text,id);
   
	   map.addOverlay(myCompOverlay);
	   
//	   map.addOverlay(marker);
	   return myCompOverlay; 
	}
/*	//设置 显示标注根据 地图的比例进行调整
      map.addEventListener("zoomend",function(a,b){
   
 });*/